package com.ts.api.module.match.plan.model;

import com.gitee.apanlh.util.base.CollUtils;
import com.ts.api.common.constant.product.ProductPlanTypeEnum;
import com.ts.api.module.api.entity.po.ApiKeyPO;
import com.ts.api.module.channel.entity.po.ChannelUserFormFieldPO;
import com.ts.api.module.channel.entity.po.ChannelUserRegisterPO;
import com.ts.api.module.match.plan.internal.core.PlanMatchChannelType;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.util.List;

/**
 *  API唤醒渠道匹配实体
 *
 *  @author Pan
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ApiWakePlanMatch extends AbstractPlanMatch {

    /** API客户端 */
    private ApiKeyPO apiKeyPO;
    /** 用户信息 */
    private ChannelUserRegisterPO channelUserRegisterPO;
    /** 用户表单 */
    private List<ChannelUserFormFieldPO> listUserForm;
    /** 本次请求id */
    private String reqId;

    /** 当前请求id */
    public ApiWakePlanMatch() {
        super();
    }

    /**
     *  开启允许多推
     *
     *  @author Pan
     */
    public void enableAllowMultiPush() {
        getPlanMatchConfig().setAllowMultiPush(true);
    }

    /**
     *  关闭允许多推
     *
     *  @author Pan
     */
    public void disableAllowMultiPush() {
        getPlanMatchConfig().setAllowMultiPush(false);
    }

    /**
     *  构建匹配实体
     *
     *  @return ApiPlanMatch
     */
    public static ApiWakePlanMatch ofMatch(String reqId, ApiKeyPO wakeApiKeyPO, ChannelUserRegisterPO userRegisterPO, List<ChannelUserFormFieldPO> listUserForm) {

        ApiWakePlanMatch apiPlanMatch = new ApiWakePlanMatch();
        apiPlanMatch.setReqId(reqId);
        apiPlanMatch.setLoanChannelId(wakeApiKeyPO.getLoanChannelId());
        apiPlanMatch.setApiKeyPO(wakeApiKeyPO);
        apiPlanMatch.setChannelUserRegisterPO(userRegisterPO);
        apiPlanMatch.setListUserForm(listUserForm);
        apiPlanMatch.setPlanMatchChannelType(PlanMatchChannelType.API_WAKE);

        //  匹配机构/平台
        List<ProductPlanTypeEnum> listPlanType = CollUtils.newArrayList(newList -> {
            newList.add(ProductPlanTypeEnum.ORG);
            newList.add(ProductPlanTypeEnum.PLATFORM);
        });
        apiPlanMatch.setListPlanType(listPlanType);

        //  超时时长15s
        apiPlanMatch.setHttpTimeout(15000);
        apiPlanMatch.setChannelPrice(new BigDecimal("0.00"));
        apiPlanMatch.setChannelShareRatio(new BigDecimal("0.00"));
        return apiPlanMatch;
    }
}
